//
//  MLApViewController.m
//  RuZe
//
//  Created by 程杰 on 2023/5/31.
//

#import "MLApViewController.h"
#import "MLBleHelp.h"
#import "SVProgressHUD.h"
@interface MLApViewController ()
@property (weak, nonatomic) IBOutlet UITextField *ssidTF;
@property (weak, nonatomic) IBOutlet UITextField *passTF;

@property (weak, nonatomic) IBOutlet UILabel *resultLb;


@end

@implementation MLApViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.resultLb.numberOfLines = 0;
    
    self.ssidTF.text = [MLBleHelp getSSID];
    self.passTF.text = @"";
}
//点击配网
- (IBAction)beginConnect:(UIButton *)sender {
    
    if (self.ssidTF.text.length <= 0 ||
        self.passTF.text.length <= 0) {
        [SVProgressHUD showInfoWithStatus:@"WiFi和密码不能为空"];
        return;
    }
    
    if (!self.bleModel) {
        [SVProgressHUD showInfoWithStatus:@"外设也断开，请重新连接"];
        return;
    }
    
//    sender.selected = !sender.selected;
//
//    if (sender.selected) {
//        [sender setBackgroundColor:[UIColor grayColor]];
//        sender.enabled = NO;
//    }
    
//    [SVProgressHUD showInfoWithStatus:@"开始配网"];
    
    int time = 60;//[self.overTimeTf.text intValue];
    NSLog(@"time is:%d",time);
    
//    [[MLBLEManager sharedManager] mlBleConnectCBPeripheralModel:self.model overTime:time];
    
//    PF和Port需要为NSNumber类型
    NSDictionary *dict = @{
        @"type": @"Wifi",
        @"data": @{
            @"Ssid": self.ssidTF.text,
            @"Pwd": self.passTF.text,
            @"PF": @(0),
            @"Host": @"",
            @"Port": @(0),
            @"PId": @"",
            @"DId": @"",
            @"BindCode": @"",
            @"Token": @"",
            @"ActUrl": @"",
            @"CertUrl": @"",
            @"BindUrl": @"",
            @"OtaUrl": @"",
            @"Res": @""
        }
    };
    
//    [[MLBLEManager sharedManager] mlBleSmartConfigWifiName:self.wifiNameLb.text passworld:self.passTf.text params:dict];
    
    MLBleParamsItem *item = [MLBleParamsItem new];
    item.wifiPassWorld = self.passTF.text;
    item.wifiName = self.ssidTF.text;
    item.bleModel = self.bleModel;
//    item.overTime = time;
    
    // 是lot的填写，其他为nil，lot格式按照如上
    item.lotDict = dict;
    
    __weak typeof(self)weakself = self;
    
    if (self.bleModel.mlApState == 0) {
        [SVProgressHUD showInfoWithStatus:@"设备已配网无需配网！"];
        return;
    }
    
    [[MLBLEManager sharedManager] mlBleConnectCBPeripheralModel:self.bleModel];
    
    [[MLBLEManager sharedManager] mlBleCBStateLinsten:^(MLBLEConnectCBState state) {
       
        if (state == MLBLEConnectCBNotifyOK) {
            
            [[MLBLEManager sharedManager] mlbleApConfig:item resultBlock:^(MLBleResultInfos * _Nonnull result) {
                weakself.resultLb.text = result.resultStr;
                int code = [result.resultDict[@"data"][@"Code"] intValue];
                if (code == 1) {
                    [SVProgressHUD showSuccessWithStatus:@"配网成功"];
                }
            }];
        }
    }];
    
}


@end
